const tbody = document.querySelector("#tbody")
//渲染页面数据
window.onload = function(){
    getStudentScoreInfo()
}
function getStudentScoreInfo(){
     //获取token
     const token = sessionStorage.getItem("token")
     axios({
         url:"http://localhost:8080/getStudentScoreInfo?token="+token,
     })
     .then(res=>{
         //axios数据在res.data中
         // console.log(res.data);
         if(res.data.code == 200){
             tbody.innerHTML = ''
             //渲染数据
             const list = res.data.data.studentScoreInfo;
             list.forEach((item,index)=>{
                 // console.log(item);
                 const str = `
                 <tr>
                     <td>${index+1}</td>
                     <td>${item.name}</td>
                     <td>${item.gender}</td>
                     <td>${item.Chinese}</td>
                     <td>${item.Math}</td>
                     <td>${item.English}</td>
                     <td>${+item.Chinese + +item.Math + +item.English}</td>
                     <td>
                     <button type="button" class="btn btn-danger btn-sm" id="${item.id}">删除</button>
                     <button type="button" data-bs-toggle="modal"
                     data-bs-target="#update" class="btn btn-warning btn-sm" id="${item.id}">修改</button>
                     </td>
               </tr>
                 `
                     tbody.innerHTML += str;
             })
                 
         }else{
             if(res.data.code == 301){
                 location.href = "../html/login.html"
             }
         }
     })
}

//使用时间委托做修改和删除
tbody.addEventListener("click",(e)=>{
    //删除学生成绩信息
    // console.log(e.target);
    if(e.target.className.includes("btn-danger")){
        const result = confirm("确定要删除该条数据吗？")
        if(result){
            const token = sessionStorage.getItem("token")
            const id = e.target.id;
            console.log(token,id);
            axios({
                url:"http://localhost:8080/delStudentScoreInfo",
                method:"POST",
                data:{
                    token,
                    id
                }
            })
            .then(res =>{
                //删除成功刷新页面
                location.reload();
            })
        }
    }

    //将数据渲染到修改学生成绩信息页面
    if(e.target.className.includes("btn-warning")){
        //获取token和id
        const token = sessionStorage.getItem("token");
        const id = e.target.id;
        //发送ajax请求
        axios({
            url:`http://localhost:8080/getStudentScoreInfoById?token=${token}&id=${id}`
        })
        .then(res=>{
            const data = res.data.data
            //获取服服务器的数据渲染到修改页面
            document.querySelector("#uid").value = data.id;
            document.querySelector("#uname").value = data.name;
            document.querySelector("#ugender").value = data.gender;
            document.querySelector("#uChinese").value = data.Chinese;
            document.querySelector("#uMath").value = data.Math;
            document.querySelector("#uEnglish").value = data.English;
        })

    }
})

//修改操作
function conformStudentScoreInfo(){
    const id = document.querySelector("#uid").value;
    const name = document.querySelector("#uname").value.trim();
    const gender = document.querySelector("#ugender").value.trim();
    const Chinese = document.querySelector("#uChinese").value.trim();
    const Math = document.querySelector("#uMath").value.trim();
    const English = document.querySelector("#uEnglish").value.trim();
    const token = sessionStorage.getItem("token")
    axios({
        url:"http://localhost:8080/upddateStudentScoreInfo",
        method:"POST",
        data:{token,id,name,gender,Chinese,Math,English}
    })
    .then(res=>{
        //修改成功刷新页面
        location.reload();
    })
}

//添加学生成绩信息
function addStudentScoreInfo(){
    const name = document.getElementById("name").value.trim();
    const gender = document.getElementById("gender").value.trim();
    const Chinese = document.getElementById("Chinese").value.trim();
    const Math = document.getElementById("Math").value.trim();
    const English = document.getElementById("English").value.trim();
    const token = sessionStorage.getItem("token")
    if(name.length && gender.length && Chinese.length && Math.length && English.length){
            axios({
                url:"http://localhost:8080/addStudentScoreInfo",
                method:"POST",
                data:{name,gender,Chinese,Math,English,token}
            })
            .then(res=>{
                location.reload();
            })
    }else{
        alert("请输入完整的学生成绩信息")
    }
}
